package com.gadgets.busi.bm02.common;

import com.gadgets.busi.bm02.pojo.MaterialDict;
import com.gadgets.busi.bm02.pojo.MaterialRecipe;
import com.gadgets.framework.common.constant.GadgetsExceptionConstant;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.CacheUtils;
import com.gadgets.framework.common.util.PropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-11-02
 *
 */
@Component
public class GadgetsBm02Utils {

    @Autowired
    private CacheUtils cacheUtils;

    /**
     * 新增MC物品字典信息到缓存
     * @param dict  字典项
     */
    public void addMcMeterialDictItem(MaterialDict dict){
        boolean result = cacheUtils.putDefualtCache(dict.getId(),dict);
        result = cacheUtils.putDefualtCache(dict.getDictName()+dict.getModName(),dict);
    }

    /**
     * 从缓存获取MC物品字典信息
     * @param dictId    字典ID
     * @return          应答信息
     */
    public MaterialDict getMcMeterialDictItem(Long dictId){
        MaterialDict dict = cacheUtils.getDefaultCache(dictId, MaterialDict.class);
        if(null==dict){
            throw new GadgetsServiceException(GadgetsExceptionConstant.VALIDATE_ERROR, MessageFormat.format("未能获取物品ID为{0}的物品信息",dictId));
        }
        return dict;
    }

    /**
     * 获取反向字典
     * @param materialName  字典名称
     * @return              字典ID
     */
    public Long getMcMeterialReDict(String materialName){
        MaterialDict dict = cacheUtils.getDefaultCache(materialName,MaterialDict.class);
        if(null==dict){
            throw new GadgetsServiceException(GadgetsExceptionConstant.VALIDATE_ERROR, MessageFormat.format("物品名{0}对应的物品不存在",materialName));
        }
        return dict.getId();
    }

    /**
     * 获取 gadgets-bm02-da.properties 的系统参数
     * @param propName          属性名
     * @return                  属性值
     * @throws IOException      I/O异常
     */
    public static String getBm02DaProperties(String propName) throws IOException {
        return PropertiesUtils.getPropertyByValue("conf/gadgets-bm02-da.properties",propName);
    }

    /**
     * 增加最小合成权值缓存对象
     * @param fastCahce     缓存信息
     */
    public void putLigtWeightRecipeCache(ConcurrentHashMap<Long, List<MaterialRecipe>> fastCahce){
        ConcurrentHashMap<Long, List<MaterialRecipe>> oriFastCahce = getListWeightRecipeCache();
        if(null==oriFastCahce) {
            cacheUtils.putDefualtCache(GadgetsBm02Constant.RECIPE_FAST_RESULT, fastCahce);
        }else {
            oriFastCahce.putAll(fastCahce);
            cacheUtils.putDefualtCache(GadgetsBm02Constant.RECIPE_FAST_RESULT, oriFastCahce);
        }
    }

    /**
     * 获取最小合成权值缓存对象
     */
    @SuppressWarnings("unchecked")
    public ConcurrentHashMap<Long, List<MaterialRecipe>> getListWeightRecipeCache(){
        return cacheUtils.getDefaultCache(GadgetsBm02Constant.RECIPE_FAST_RESULT,ConcurrentHashMap.class);
    }

}
